﻿@model List<LbangCMS.Areas.Admin.Models.PropertyModel>
@{
    ViewBag.Title = "PropertyList";
    Layout = "~/Areas/Admin/Views/Shared/_FormLayout.cshtml";
}


@section head{
    <style>
        .btn {
            padding: 2px 12px;
        }

        .panel-body {
            padding: 10px 0px;
        }
    </style>
    <link href="~/Areas/Admin/Content/libs/zui/lib/datatable/zui.datatable.css" rel="stylesheet" />

}
@section script{
    <script src="~/Areas/Admin/Content/libs/zui/lib/datatable/zui.datatable.js"></script>
    <script type="text/javascript">
        function to_datatable() {
            $('table.datatable').datatable({
                checkable: false,
                checkByClickRow: false,
                fixedHeader: false
            });
        }
        to_datatable();

        function reload_data()
        {
            var ProductId = $("#ProductId").val();
            var url = '@Url.Action("PropertyList", "Product")' + "?ProductId=" + ProductId + "&r=" + Math.random();
            $.get(url,{}, function (data) {
                $("#view").html(data);
                to_datatable();
            },"html")
        }

        function Remove(id)
        {
            layer.confirm('确定要移除吗?', { icon: 3, title: '提示' }, function (index) {
                var url = '@Url.Action("RemoveProperty", "Product")';
                $.post(url, { id: id }, function (data) {
                    layer.msg("删除成功", { time: 800 });
                    reload_data();
                })
            });
        }

        function UpdateProperty(obj)
        {
            var tr = $(obj).parents("tr");
            var id = tr.find("input.itemId").eq(0).val();
            var propertyName = tr.find("input.propertyName").val();
            var propertyValue = tr.find("input.propertyValue").val();
            var sortIndex = tr.find("input.sortIndex").val();
            if (propertyName.length <= 0)
            {
                layer.msg("请输入属性名");
                return;
            }
            if (propertyValue.length <= 0) {
                layer.msg("请输入属性值");
                return;
            }
            if (isNaN(sortIndex))
            {
                layer.msg("排序号输入有误");
                return;
            }
            else
            {
                $.post("@Url.Action("UpdateProperty", "Product")", { id: id, propertyName: propertyName, propertyValue: propertyValue, sortIndex: sortIndex }, function (data) {
                    if(data.success)
                    {
                        layer.msg("保存成功");
                    }
                });
            }


        }

        function addProperty() {
            var productId = $("#ProductId").val();
            var propertyName = $("#propertyName").val();
            var propertyValue = $("#propertyValue").val();
            var sortIndex = $("#sortIndex").val();

            if (isNaN(productId)) {
                layer.msg("请正确排序号");
                return;
            }
            else {
                $.post("@Url.Action("AddProperty", "Product")", { productId: productId, propertyName: propertyName, propertyValue: propertyValue, sortIndex: sortIndex }, function (data) {
                    if (data.success) {
                        $("#propertyName").val("");
                        $("#propertyValue").val("");
                        $("#sortIndex").val("");
                        reload_data();
                    }
                    else {
                        layer.msg(data.msg);
                    }
                })
            }


        }
    </script>
}
<div class="panel">
    <div class="panel-heading">
        <input type="hidden" id="ProductId" value="@ViewBag.ProductId" />
        <span>属性名：</span><input type="text" id="propertyName" value="" style="width:150px;height:22px" />
        &nbsp;
        &nbsp;
        <span>属性值：</span><input type="text" id="propertyValue" value="" style="width:150px;height:22px" />
        &nbsp;
        &nbsp;
        <span>排序号：</span><input type="text" id="sortIndex" value="" style="width:60px;height:22px" />
        &nbsp;
        &nbsp;
        <button type="button" class="btn" style="margin-top:-4px" onclick="addProperty()">添加</button>
    </div>
    <div class="panel-body">
        <div id="view">
            @{Html.RenderPartial("_PropertyListPartial", Model);}
        </div>
    </div>
</div>

